package com.example.springboot.Transaction.Service;

import com.example.springboot.Transaction.Config.DataSourceConfig;
import com.example.springboot.Transaction.Dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;


// 编程式事务
@Component
@Service
public class StudentService_01 {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private DataSourceConfig dataSourceConfig;

    // PlatformTransactionManager
    public void test_01() {

        //定义一个某个框架平台的TransactionManager，如JDBC、Hibernate
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();

        // 设置数据源
        dataSourceTransactionManager.setDataSource((DataSource) dataSourceConfig.dataSource());

        // 定义事务属性
        DefaultTransactionDefinition transDef = new DefaultTransactionDefinition();

        // 设置传播行为属性
        transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);

        // 获得事务状态
        TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef);

        try {

            studentDao.updateAgeById(11,1);
            int i=1/0; //这里故意让他报错
            System.out.println("-----------");
            studentDao.updateNameById("test11",1);
            // 数据库操作
            dataSourceTransactionManager.commit(status);// 提交
        } catch (Exception e) {
            dataSourceTransactionManager.rollback(status);// 回滚
        }
    }

    // TransactionTemplate
    public void test_02() {

        //定义一个某个框架平台的TransactionManager，如JDBC、Hibernate
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();

        // 设置数据源
        dataSourceTransactionManager.setDataSource((DataSource) dataSourceConfig.dataSource());

        // 创建TransactionTemplate
        TransactionTemplate transactionTemplate = new TransactionTemplate();

        // 给TransactionTemplate设置数据源
        transactionTemplate.setTransactionManager(dataSourceTransactionManager);

        // 设置事务传播行为
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        // 执行
        transactionTemplate.execute(
                new TransactionCallbackWithoutResult() {
                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) {
                        try {
                            studentDao.updateAgeById(111,1);
                            int i=1/0; //这里故意让他报错
                            System.out.println("-----------");
                            studentDao.updateNameById("test111",1);
                        } catch (Exception e) {
                            status.setRollbackOnly(); //设置事务位回滚状态
                            throw e;
                        }
                    }
                }
        );


    }

}
